package com.molichuxing.services.infrastructure.service.impl;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.molichuxing.services.infrastructure.bean.EnterpriseCarTransferBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.EnterpriseCarTransferMapper;
import com.molichuxing.services.infrastructure.dto.request.create.EnterpriseCarTransferCreateDto;
import com.molichuxing.services.infrastructure.dto.response.EnterpriseCarTransferDto;
import com.molichuxing.services.infrastructure.service.EnterpriseCarTransferService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 企业车辆过户实现
 *
 * @author liyiguang
 * @date 2020年6月4日
 */
@Service("enterpriseCarTransferService")
public class EnterpriseCarTransferServiceImpl implements EnterpriseCarTransferService {

	private static final Logger logger = LoggerFactory.getLogger(EnterpriseCarTransferServiceImpl.class);

	@Resource
	private EnterpriseCarTransferMapper enterpriseCarTransferMapper;

	/**
	 * 新增企业车辆过户信息
	 * @param createDto
	 * @return
	 */
	@Override
	public Boolean create(EnterpriseCarTransferCreateDto createDto) {
		EnterpriseCarTransferBean bean =new EnterpriseCarTransferBean();
		BeanUtils.copyProperties(createDto, bean);
		int num = enterpriseCarTransferMapper.create(bean);
		return num==1;
	}
	
	 /**
	  * 根据车架号查询过户信息
	  * @param vin
	  * @return
	  */
	@Override
	public EnterpriseCarTransferDto getEnterpriseCarTransferByVin(String vin) {
		EnterpriseCarTransferBean bean = enterpriseCarTransferMapper.getEnterpriseCarTransferByVin(vin);
		return Convert.toEnterpriseCarTransferDto(bean);
	}

	@Override
	public List<EnterpriseCarTransferDto> getBytransferTime(Map<String, Object> params) {
		List<EnterpriseCarTransferDto> res = new ArrayList<>();
		List<EnterpriseCarTransferBean> list= enterpriseCarTransferMapper.getBytransferTime(params);
		if(list!=null && list.size()>0){
			for (EnterpriseCarTransferBean bean : list) {
				if(bean!=null ){
					EnterpriseCarTransferDto dto = Convert.toEnterpriseCarTransferDto(bean);
					res.add(dto);
				}
			}
		}
		return res;
	}

	@Override
	public Map<Integer, EnterpriseCarTransferDto> getByDto(List<EnterpriseCarTransferDto> transferedDtoList) {

		if(transferedDtoList==null || transferedDtoList.size()<1){
			return null;
		}
		logger.info("获取过户信息:"+transferedDtoList.toString());

		Map<Integer,EnterpriseCarTransferDto> res = new HashMap<>();
		List<EnterpriseCarTransferBean> list = enterpriseCarTransferMapper.getByDto(transferedDtoList);
		if(list!=null && list.size()>0){
			for (EnterpriseCarTransferBean bean : list) {
				if(bean!=null ){
					EnterpriseCarTransferDto dto = Convert.toEnterpriseCarTransferDto(bean);
					res.put(bean.getCarId(),dto);
				}
			}
		}
		logger.info("获取过户信息结果:"+res);
		return res;
	}

}
